﻿/*----------------------------------------------------------------
//Copyright (C) 2012 赵国伟
//文件功能描述：商品推荐管理客户端脚本
//创建人：赵国伟
//生成时间：2012-04-21 10:47:48
//----------------------------------------------------------------*/
var AjaxServerUrl = "/Manage/AS/GoodsCommon.aspx";
var ModelOfGoodsCommend;
var ModelOfIDNameObj;
var GoodsCommendID = 0;
var SelectOptionOfCategory;
var ctrlOfSelectGoods; //商品选择控件。
var SWFUploader = null; //上传控件；(SwfUpload)
var SessionID = "";
var CommendImgUrl = "";


///初始化
function Init() {
    GoodsCommendID = 0;
    CommendImgUrl = "";
}

function GoToSearch() {
    var url = "CommendManage.aspx?";
    url += "keyWords=" + escape($("#txtKeyWords").val().trim());
    url += "&CategoryID=" + $("#ddlCategoryIDOfSearch").val();
    location.href = url;
}

//执行删除
function Delete(id) {
    if (!confirm("系统提示：您确定要删除该条商品推荐信息？"))
        return;
    AjaxGet(AjaxServerUrl + "?Action=DeleteGoodsCommend&GoodsCommendID=" + id);
}

///检查表单；
function CheckForm() {
    if ($("#ddlCategoryID").val() == "") {
        alert("系统提示：请选择推荐类别！");
        $("#ddlCategoryID").focus();
        return false;
    }
    if(ctrlOfSelectGoods.SelectValue() == "") {
        alert("提示：请选择推荐的商品！");
        $("#btnSelGoods").focus();
        return false;
    }
    if ($("#txtOrderValue").val() == "") {
        alert("系统提示：请输入排序值！");
        $("#txtOrderValue").focus();
        return false;
    }
    if (!IsNumber($("#txtOrderValue").val())) {
        alert("系统提示：排序值必须为数字！");
        $("#txtOrderValue").focus();
        return false;
    }
    return true;
}

//保存；
function Save() {
    $("#btnSave").attr("disabled", true);
    if (!CheckForm()) {
        $("#btnSave").attr("disabled", false);
        return;
    }
    var data = clone(ModelOfGoodsCommend);
    data._goodscommendid = GoodsCommendID;
    data._categoryid =$("#ddlCategoryID").val();
    data._commenddate = "/Date(" + CurrentDateStringToUTC() + ")/";
    data._goodsid = ctrlOfSelectGoods.SelectValue();
    data._ordervalue = $("#txtOrderValue").val();
    data._commendtitle = $("#txtCommendTitle").val();
    data._commendmemo = $("#txtCommendMemo").val();
    data._commendimg = CommendImgUrl;
    var url = AjaxServerUrl + "?Action=SaveGoodsCommend";
    AjaxPost(url, "btnSave", data);  //更新
}

//绑定商品推荐信息到录入表单中；
function BindGoodsCommendToForm() {
    var url = AjaxServerUrl + "?action=GetGoodsCommend&GoodsCommendID=" + GoodsCommendID + "&tttt=" + Math.random();
    $.ajax(
    {
        type: "Get",
        url: url,
        dataType: "json",
        success: function (data) {
            if (data != null) {
                $("#ddlCategoryID").val(data.CommendInfo._categoryid);
                $("#txtOrderValue").val(data.CommendInfo._ordervalue);
                $("#txtCommendTitle").val(data.CommendInfo._commendtitle);
                $("#txtCommendMemo").val(data.CommendInfo._commendmemo);
                if (data.CommendInfo._commendimg + "" != "")
                    SetUploadShow(data.CommendInfo._commendimg);

                var ds = new Array();
                var d = clone(ModelOfIDNameObj);
                d._id = data.CommendInfo._goodsid;
                d._name = data.GoodsName;
                ds.push(d);
                ctrlOfSelectGoods.SetDefaultValue(ds);
            }
        }
    });
}

///添加界面；
function Add() {
    Init();
    GetPopupDiv(700, 800, "#e2e2e2", "添加商品推荐", GetHtmlForm());
    ctrlOfSelectGoods = new SelectGoods("btnSelGoods", "Single", "spGoods", "");
    SWFUploader = CreateSwfUploadObject("/Manage/AS/AjaxUpload.aspx?action=UploadFiles", SessionID, "*.jpg;*.gif;*.png", "图片文件", "spUploadBtn", "spCommendImg");
}

//编辑界面；
function Edit(id) {
    Init();
    GoodsCommendID = id;
    GetPopupDiv(700, 800, "#e2e2e2", "编辑商品推荐", GetHtmlForm());
    ctrlOfSelectGoods = new SelectGoods("btnSelGoods", "Single", "spGoods", "");
    SWFUploader = CreateSwfUploadObject("/Manage/AS/AjaxUpload.aspx?action=UploadFiles", SessionID, "*.jpg;*.gif;*.png", "图片文件", "spUploadBtn", "spCommendImg");
    BindGoodsCommendToForm();
}


///设置上传成功后的图片显示。
function SetUploadShow(fileUrl) {
    //往外赋值；
    $("#trCommendImg").show();
    CommendImgUrl = fileUrl;
    $("#spCommendImg").html("<a href=\"" + fileUrl + "\" target=\"_blank\"><img src=\"" + fileUrl + "\" ></a> [ <a href=\"javascript:void(0);\" onclick=\"DeleteUpload();\" class=\"red\">删除</a> ]");
}

///品牌图片上传成功后的处理；
function uploadSuccess(file, fileUrl) {
    try {
        SetUploadShow(fileUrl);
    } catch (ex) {
        this.debug(ex);
    }
}

function DeleteUpload() {
    CommendImgUrl = "";
    $("#spCommendImg").html("");
    $("#trCommendImg").hide();
}

///获取录入界面的HTML;
function GetHtmlForm() {
    var html = new Array();
    html.push("<form>");
    html.push("<table width=\"700\" border=\"0\" cellspacing=\"0\" cellpadding=\"0\" class=\"table\">");
    html.push("  <tr>");
    html.push("    <td colspan=\"2\" class=\"TableHeadRow\">&gt;&gt;商品推荐</td>");
    html.push("  </tr>");
    html.push("  <tr>");
    html.push("    <td width=\"90\" align=\"right\">推荐类别：</td>");
    html.push("    <td ><select name=\"ddlCategoryID\" id=\"ddlCategoryID\">");
    html.push("      <option value=\"\">--请选择推荐类别</option>");
    html.push(SelectOptionOfCategory);
    html.push("    </select>");
    html.push("    <span class=\"red\">(*)</span></td>");
    html.push("  </tr>");
    html.push("  <tr>");
    html.push("    <td align=\"right\">推荐商品：</td>");
    html.push("    <td>");
    html.push("      <input name=\"btnSelGoods\" type=\"button\" id=\"btnSelGoods\" value=\"请选择商品..\" class=\"btn2\"/><span class=\"red\">(*)</span>");
    html.push("&nbsp;&nbsp; <span id=\"spGoods\"></span>");
    html.push("      </td>");
    html.push("  </tr>");

    html.push("  <tr>");
    html.push("    <td align=\"right\">推荐标题：</td>");
    html.push("    <td><input name=\"txtCommendTitle\" type=\"text\" class=\"border6\" id=\"txtCommendTitle\" value=\"\" size=\"70\" />");
    html.push("      <span class=\"red\">不填显示商品名称</span></td>");
    html.push("  </tr>");

    html.push("  <tr>");
    html.push("    <td align=\"right\">推荐描述：</td>");
    html.push("    <td><textarea name=\"txtCommendMemo\" cols=\"60\" rows=\"4\" class=\"border6\" id=\"txtCommendMemo\"></textarea></td>");
    html.push("  </tr>");

    html.push("  <tr>");
    html.push("    <td align=\"right\">推荐图片：</td>");
    html.push("    <td><span id=\"spUploadBtn\"></span></td>");
    html.push("  </tr>");
    html.push("  <tr id=\"trCommendImg\" style=\"display:none;\">");
    html.push("    <td align=\"right\">&nbsp;</td>");
    html.push("    <td><span id=\"spCommendImg\"></span>&nbsp;</td>");
    html.push("  </tr>");


    html.push("  <tr>");
    html.push("    <td align=\"right\">排序值：</td>");
    html.push("    <td><input name=\"txtOrderValue\" type=\"text\" class=\"border6\" id=\"txtOrderValue\" value=\"0\" size=\"5\" />");
    html.push("      <span class=\"red\">(*)</span></td>");
    html.push("  </tr>");
    html.push("  <tr>");
    html.push("    <td align=\"right\">&nbsp;</td>");
    html.push("    <td><input name=\"btnSave\" type=\"button\" id=\"btnSave\" value=\" 保 存 \" class=\"btn2\" onclick=\"Save();\">");
    html.push("    <input name=\"btnReset\" type=\"reset\" id=\"btnReset\" value=\" 重 置 \" class=\"btn2\">");
    html.push("    <input name=\"btnClose\" type=\"button\" id=\"btnClose\" value=\"关闭本窗口\" class=\"btn2\" onclick=\"yesOther(false);\"></td>");
    html.push("  </tr>");
    html.push("</table>");
    html.push("</form>");
    return html.join("");
}




function yesOther(t) {
    if (SWFUploader != null) {
        SWFUploader.destroy();
    }
    var IsHavePopDiv = false;
    var lastDivName = "";
    var divs = document.getElementsByTagName("DIV");
    for (var i = 0; i < divs.length; i++) {
        if (divs[i].id.indexOf(prePopDivName) >= 0) {
            fullPopDivName = divs[i].id;
        }
    }
    for (var i = 0; i < divs.length; i++) {
        if (divs[i].id.indexOf(prePopDivName) >= 0 && divs[i].id != fullPopDivName) {
            oo(divs[i].id).style.display = "";
            IsHavePopDiv = true;
        }
    }
    if (!t) {
        var bgObj2 = document.getElementById(fullPopDivName);
        document.body.removeChild(bgObj2);
        if (!IsHavePopDiv) {
            var bgObj = document.getElementById("bgDiv");
            document.body.removeChild(bgObj);
        }
    }
    else {
        var bgObj2 = document.getElementById(fullPopDivName);
        document.body.removeChild(bgObj2);
        if (!IsHavePopDiv) {
            var bgObj = document.getElementById("bgDiv");
            document.body.removeChild(bgObj);
        }
    }
    for (var i = 0; i < divs.length; i++) {
        if (divs[i].id.indexOf(prePopDivName) >= 0) {
            fullPopDivName = divs[i].id;
        }
    }
}










